﻿using BusinessEntities.Model;
using BusinessLayer;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebFrontEnd
{
    public partial class EventsParent : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (HttpContext.Current.User.Identity.IsAuthenticated)
            {
                var u = GetCurrentUser();
                loggedInName.InnerText = u.FirstName;
                var events = BL.GetUsersEvents(u);
                if (events.Count() <= 0)
                {
                    eventSubmenuContainer.Attributes["class"] = "disabled";     
                }
                else
                {
                    string eventListItemFormat = @"<li><a href=""Event.aspx?eventID={0}"">{1}</a></li>";
                    StringBuilder innerHtml = new StringBuilder("");
                    eventSubmenuContainer.Attributes["class"] = "dropdown-submenu";
                    foreach (BusinessEntities.Model.Event ev in events)
                    {
                        innerHtml.Append(string.Format(eventListItemFormat, ev.ID,ev.Name));
                    }
                    eventSubmenu.InnerHtml = innerHtml.ToString();
                }
            }
        }
        #region Protected Members
        protected IBL _bl;
        #endregion

        #region Protected Properties
        protected IBL BL
        {
            get
            {
                if (_bl == null)
                {
                    _bl = BusinessLayerFactory.GetCurrentBusinessLayer(ConfigurationManager.AppSettings.Get("DomainName"));
                }
                return _bl;
            }
        }
        #endregion

        #region Protected Methods
        protected User GetCurrentUser()
        {
            return BL.GetUserByEmail(HttpContext.Current.User.Identity.Name);
        }
        #endregion
    }
}